package ModelLayer;

import java.util.ArrayList;
/**
 * Write a description of class AddressBook here.
 * 
 * @author (Jamil) 
 * @version (a version number or a date)
 */
public class AddressBook
{

     private ArrayList<Person> persons;
     private static AddressBook instance;
    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        persons = new ArrayList<Person>();
    }
    
    public static AddressBook getInstance()
    {
        if (instance == null) {
            instance = new AddressBook();
        }
        return instance;
    }

    /**
     * Returns ArrayList persons
     * aka getPersons
     */
    public ArrayList<Person> listAllPersons()
    {
        return persons;
    }
   
    /**
     * Adds a person to the ArrayList person
     * @param a Person
     */
    public void addPerson(Person p)
    {
      persons.add(p);
    }

    /**
     * finds a person in ArrayList persons based on the persons id
     * @param long The id of the wanted person
     * @return Person the person found in the ArrayList
     */
    public Person getPerson(long id)
    {
        int index = 0;
        
        while (index < persons.size()) {
            Person p = persons.get(index);
            
            if (p.getId() == id){
                return p;
            }
            index++;
        }
       return null;
    }
    
    /**
     * deletes a person from the ArrayList, based on the persons id
     * @param long The id of the wanted person
     */
    public void deletePerson(long id)
    {
        int index = 0;
        boolean found = false;
        
        while (index < persons.size() && !found) {
            Person p = persons.get(index);
            
            if (p.getId() == id) {
            found = true;
            persons.remove(index);
            }
        index++;
        }
    }
    
    /**
     * updates the information stored on a person. The person is found via the id, using the getPerson()
     * @param long, String, String, int, String, int 
     */
    public void updatePerson(long id, String name, String address, int zipCode, String city, int phone)
    {
        Person p = getPerson(id);
        
        if(name != "") { p.setName(name);}
        if(address != "") { p.setAddress(address);}
        if(zipCode != 0) { p.setZipCode(zipCode);}
        if(city != "") { p.setCity(city);}
        if(phone != 0) {p.setPhone(phone);}
            
    }
}
